<?php
/**
 * User: zy
 * Date: 2018-06-25 14:10:30
 */
class SupplierContract extends Eloquent
{
    protected $table = "supplier_contract";
    
    /**
     * @var array  合同类型数组
     */
    public static $type_arr = [
        '1' => '配件',
        '2' => '商品',
        '3' => '设备',
        '4' => '维修服务',
    ];
    
    /**
     * @var array  是否延期数组
     */
    public static $delay_status_arr = [
        '1' => '无需延期',
        '2' => '已延期',
        '3' => '到期不延期',
    ];
    
    /**
     * @var array  合同状态数组
     */
    public static $contract_status_arr = [
        '2' => '有效',
        '3' => '无效',
        '3' => '已延期',
    ];
    
    /**
     * @Author:      zy
     * @DateTime:    2018-06-26 15:35:20
     * @Description: 更新供应商的合同状态
     */
    public static function supplierContractStatus($supplier_id){
        $date = date("Y-m-d");
        $supplier = MaintenanceSupplier::find($supplier_id);
        $count = SupplierContract::where('supplier_id',$supplier->id)->count(); //合同列表
        if($count == 0){ //如果没有合同,供应商合同状态为未签
            $supplier->contract_status = 1;
            $supplier->save();
        }else{ //有合同的状态下
            $contract = SupplierContract::where('supplier_id',$supplier->id)->get(); //合同列表
            foreach ($contract as  $_c) {
                if ($_c->over_date >= $date) { //如果还没有到中止日期为有效
                    $_c->contract_status = 2;
                }else{//到了终止日期
                    if ($_c->delay_status == 2) { //如果是已延期
                        $_c->contract_status = 4;
                    }else{//无需延期和到期不延期 为无效
                        $_c->contract_status = 3;
                    }
                }
                $_c->save();
            }
            $new_contract = SupplierContract::where('supplier_id',$supplier->id)
                    ->orderBy("created_at",'DESC')
                    ->first();//按创建时间，取最新的合同信息进行判断
            $supplier->contract_status = $new_contract->contract_status;
            $supplier->save();
        }
    }
}